﻿Imports System.IO
Imports System.Data
Imports MetodosComuns
Imports extranet.BC
Imports extranet.BC.BCRecado
Imports extranet.BE
Imports System.Collections.Generic

Partial Class Perfis
  Inherits System.Web.UI.Page

  Private Const DEBUG As Boolean = False

  Private Const SORT_INDICE_ARRAY As Integer = 0
  Private Const SORT_REGISTROS As Integer = 0

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '==================       PROCEDIMENTO  PADRÃO  SEGURANÇA     =============================
    Dim objBESeguranca As BESeguranca
    Dim objBEUsuario As BEUsuario = Session("objBEUsuario")
    If IsNothing(objBEUsuario) Then Response.Redirect(TELA_LOGOUT)
    Session("strTela") = MetodosComuns.GetCurrentPageName()
    If Not IsPostBack Then          ' SEGURANÇA
      objBESeguranca = BCSeguranca.SelecionaPermissoes(objBEUsuario.GrupoAcesso, Session("strTela"))
      If Not objBESeguranca.Autorizado Then Response.Redirect(TELA_LOGOUT)
      Session("objBESeguranca") = objBESeguranca
    Else
      objBESeguranca = Session("objBESeguranca")
    End If

    '==================       ALTERAR SE FOR NECESSARIO          ===============================
    If Not IsPostBack Then
      Dim sortExpression(SORT_INDICE_ARRAY) As String
      Dim sortDirection(SORT_INDICE_ARRAY) As String
      Session(Session("strTela") & "_SortExpression") = sortExpression
      Session(Session("strTela") & "_SortDirection") = sortDirection

      Session("PagerPos") = 1
      ddl_Franquia.DataSource = BCFranquia.Selecionar()
      ddl_Franquia.DataBind()
      mostraPaineis(ESTADO_INICIAL)
    End If

  End Sub

  Protected Sub mostraPaineis(ByVal estado As Int16)
    Dim objBESeguranca As BESeguranca = DirectCast(Session("objBESeguranca"), BESeguranca)

    ' SEGURANÇA
    Select Case estado
      Case ESTADO_INICIAL, ESTADO_BUSCA, ESTADO_LISTA
        If Not objBESeguranca.Leitura Then estado = IIf(objBESeguranca.Escrita, ESTADO_CADASTRO, ESTADO_BLOQUEADO)
      Case ESTADO_DETALHE
      Case ESTADO_EDICAO
      Case ESTADO_CADASTRO
    End Select

    ' Mostra Painel segundo o estado
    If Not DEBUG Then
      Select Case estado
        Case ESTADO_BLOQUEADO
          panBuscaRegistro.Visible = False
          pangrvRegistros.Visible = False
        Case ESTADO_INICIAL, ESTADO_BUSCA, ESTADO_LISTA
          panBuscaRegistro.Visible = True
          pangrvRegistros.Visible = True
        Case ESTADO_DETALHE, ESTADO_EDICAO, ESTADO_CADASTRO
          panBuscaRegistro.Visible = False
          pangrvRegistros.Visible = False
      End Select
    Else
      panBuscaRegistro.Visible = True
      pangrvRegistros.Visible = True
    End If

    ' Set Registro(s) segundo o estado
    Select Case estado
      Case ESTADO_INICIAL, ESTADO_BUSCA, ESTADO_LISTA
        mostraRegistros()
      Case ESTADO_DETALHE, ESTADO_EDICAO
      Case ESTADO_CADASTRO
    End Select

    ' Set Focus segundo o Estado
    Select Case estado
      Case ESTADO_INICIAL, ESTADO_BUSCA, ESTADO_LISTA
        scriptManager.SetFocus(txtNome)
      Case ESTADO_DETALHE
      Case ESTADO_EDICAO
      Case ESTADO_CADASTRO
    End Select

  End Sub

  Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
    Session("PagerPos") = 1
    mostraPaineis(ESTADO_LISTA)
  End Sub

  Protected Sub mostraRegistros()
    dtlRegistros.DataSource = PagerControl(BCUsuario.Selecionar(txtNome.Text, ddl_Franquia.SelectedValue))
    dtlRegistros.DataBind()
  End Sub

  Protected Sub MudaPagina(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("PagerPos") = Int(DirectCast(sender, LinkButton).CommandArgument)
    mostraRegistros()
  End Sub

  Protected Function PagerControl(ByVal records As Object) As Object
    Dim intPage As Integer = Session("PagerPos")
    Dim intPageSize As Integer = 10
    Dim intRecordSize As Integer = 10
    Dim lstRecords As IList(Of BEUsuario) = records
    Dim newRecords As List(Of BEUsuario) = New List(Of BEUsuario)
    Dim intPageTotal As Integer = Int((lstRecords.Count() + intRecordSize - 1) / intRecordSize)
    Dim intPageInicial As Integer = Int((intPage - 1) / intPageSize) * intPageSize + 1
    Dim intPageFinal As Integer = Math.Min(intPageInicial + intPageSize - 1, intPageTotal)
    Dim intRecordInicial As Integer = (intPage - 1) * intRecordSize
    Dim intRecordFinal As Integer = Math.Min(intRecordInicial + intRecordSize, lstRecords.Count()) - 1
    Dim booMostra As Boolean = False

    lnk_page00.CommandArgument = intPageInicial - 1
    div_page00.Visible = (intPage > intPageSize)
    lnk_page00.Visible = (intPage > intPageSize)

    booMostra = (intPageInicial <= intPageFinal)
    div_page01.Visible = booMostra
    lnk_page01.Visible = booMostra
    lnk_page01.Text = intPageInicial
    lnk_page01.CommandArgument = intPageInicial
    lnk_page01.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page02.Visible = booMostra
    lnk_page02.Visible = booMostra
    lnk_page02.Text = intPageInicial
    lnk_page02.CommandArgument = intPageInicial
    lnk_page02.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page03.Visible = booMostra
    lnk_page03.Visible = booMostra
    lnk_page03.Text = intPageInicial
    lnk_page03.CommandArgument = intPageInicial
    lnk_page03.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page04.Visible = booMostra
    lnk_page04.Visible = booMostra
    lnk_page04.Text = intPageInicial
    lnk_page04.CommandArgument = intPageInicial
    lnk_page04.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page05.Visible = booMostra
    lnk_page05.Visible = booMostra
    lnk_page05.Text = intPageInicial
    lnk_page05.CommandArgument = intPageInicial
    lnk_page05.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page06.Visible = booMostra
    lnk_page06.Visible = booMostra
    lnk_page06.Text = intPageInicial
    lnk_page06.CommandArgument = intPageInicial
    lnk_page06.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page07.Visible = booMostra
    lnk_page07.Visible = booMostra
    lnk_page07.Text = intPageInicial
    lnk_page07.CommandArgument = intPageInicial
    lnk_page07.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page08.Visible = booMostra
    lnk_page08.Visible = booMostra
    lnk_page08.Text = intPageInicial
    lnk_page08.CommandArgument = intPageInicial
    lnk_page08.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page09.Visible = booMostra
    lnk_page09.Visible = booMostra
    lnk_page09.Text = intPageInicial
    lnk_page09.CommandArgument = intPageInicial
    lnk_page09.Enabled = (Not intPageInicial = intPage)

    intPageInicial += 1
    booMostra = (intPageInicial <= intPageFinal)
    div_page10.Visible = booMostra
    lnk_page10.Visible = booMostra
    lnk_page10.Text = intPageInicial
    lnk_page10.CommandArgument = intPageInicial
    lnk_page10.Enabled = (Not intPageInicial = intPage)

    lnk_page11.CommandArgument = intPageFinal + 1
    div_page11.Visible = (lstRecords.Count() > intPageFinal * intRecordSize)
    lnk_page11.Visible = (lstRecords.Count() > intPageFinal * intRecordSize)

    For intI As Integer = intRecordInicial To intRecordFinal
      newRecords.Add(lstRecords(intI))
    Next
    Return newRecords
  End Function

  Protected Sub dtlRegistros_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtlRegistros.ItemCommand
    If e.CommandName.ToUpper() = "ACESSARPERFIL" Then
      Session("UsuarioParaConsulta") = e.CommandArgument
      Response.Redirect("~/Perfil.aspx")
    End If
  End Sub
End Class
